#ifndef MODEL_H
#define MODEL_H

#include <qglobal.h>
#include "abstractmodelsubject.h"
#include "Pixel.h"
#include "PixelBuffer.h"
#include "FilterProcessor.h"
#include "ImageHistogram.h"

enum ResizeDirectMappingMode
{
    Repeat,
    Interpolate
};

enum ResizeReverseMappingMode
{
    Order0,
    Bilinear
};

enum SegmentationType
{
	BasicGlobal,
	OptimumGlobal,
	Adaptive
};

class Model : public AbstractModelSubject
{
private:
    PixelBuffer* image, *tmpBuffer;
	ImageHistogram* histogram;
    void ResizeDirectMapping_Repeat(int factor);
    void ResizeDirectMapping_Interp(int factor);
    void ResizeReverseMapping_Order0(int newWidth, int newHeight);
    void ResizeReverseMapping_Bilinear(int newWidth, int newHeight);
	int BasicGlobalThresholding(double eps);
	int OptimumGlobalThresholding();
	void AdaptiveThresholding(double winArea);
	void AdaptiveThresholding(int h, int v, int width, int height, double winArea);
	void Spilit(int h, int v, int width, int height, int T);
	void GetWindowHistogram(int h, int v, int width, int height, int& minGrayVal, int& maxGrayVal, int grayHistogram[]);
	int GetBasicGlobalThreshold(double initialT, int *grayHistogram, double eps);	
	int DoContrast(int oldVal, int newMin, int newMax, int oldMin, int oldMax);
	int CutOff(int val, int mnRange, int mxRange);
public:
    Model();
    ~Model();
    PixelBuffer* Image();
	ImageHistogram* Histogram();
    bool LoadImage(QString& path);
   
	void CopyTmpBuffer();
	void Brightness(int value);
	void Contrast(int value);
    void Inverse();
    void Grayscale();
    void ResizeDirectMapping(int factor, ResizeDirectMappingMode mode);
    void ResizeReverseMapping(int newWidth, int newHeight, ResizeReverseMappingMode mode);
    void ApplyFilter(FilterType filterType, void* filterParameter = NULL);
    void FlipVertically();
    void FlipHorizotally();
    void Translate(int dX, int dY);
    void Rotate(float theta);
	int Segment(SegmentationType sType, double param);
};

#endif // MODEL_H
